package com.example.myapp.base;

import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author weng
 * @date 2022/8/10 9:05
 * @describe 测试一下点击空白处关闭软键盘
 */
public class BaseActivity extends AppCompatActivity {


//    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
//        Log.e("tag","事件---》"+ev.getAction());

//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            View currentFocus = getCurrentFocus();
//            if (shouldHideKeyboard(currentFocus, ev)) {
//                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
//                if (imm != null) {
//                    Log.e("tag","关闭软键盘");
//                    imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
//                }
//            }
//            return super.dispatchTouchEvent(ev);
//        }
//        //noinspection SimplifiableIfStatement 必不可少，否则所有的组件都不会有TouchEvent了
//        if (getWindow().superDispatchTouchEvent(ev)) {
//            return true;
//        }
//        return onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    private boolean shouldHideKeyboard(View view, MotionEvent event) {
        if (view instanceof EditText) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            view.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + view.getHeight();
            int right = left + view.getWidth();
            Log.e("tag","view.getHeight()---》"+view.getHeight());
            Log.e("tag","view.getWidht---->"+view.getWidth());
            Log.e("tag","right---》"+right);
            Log.e("tag","bottom---->"+bottom);
            //noinspection RedundantIfStatement
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        hide(this,ev);
//        return super.dispatchTouchEvent(ev);
//    }
//
//    /**
//     * 隐藏软键盘
//     * @param activity
//     * @param ev
//     */
//    public static void hide(Activity activity, MotionEvent ev){
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            View view = activity.getCurrentFocus();
//            if (isHideInput(view, ev)) {
//                HideSoftInput(activity,view.getWindowToken());
//            }
//        }
//    }
//
//
//
//    // 判定是否需要隐藏
//    private static boolean isHideInput(View v, MotionEvent ev) {
//        if (v instanceof EditText) {
//            int[] l = { 0, 0 };
//            v.getLocationInWindow(l);
//            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
//                    + v.getWidth();
//            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
//                    && ev.getY() < bottom) {
//                return false;
//            } else {
//                return true;
//            }
//        }
//        return false;
//    }
//    // 隐藏软键盘
//    private static void HideSoftInput(Activity activity, IBinder token) {
//        if (token != null) {
//            InputMethodManager manager = (InputMethodManager)
//                    activity.getSystemService(Context.INPUT_METHOD_SERVICE);
//            manager.hideSoftInputFromWindow(token,
//                    InputMethodManager.HIDE_NOT_ALWAYS);
//        }
//    }


}
